import axios from 'axios'
import vue from "../main";

const service = axios.create({
    baseURL: '/api',
    timeout: 100000
})


//添加请求拦截器
service.interceptors.request.use((config) => {
    //在请求之前做些什么(获取并设置token)
    // config.headers['token'] = getToken('token');

    return config
}, (error => {
    return Promise.reject(error)
}))

//添加响应拦截器
service.interceptors.response.use((response) => {
    //对响应数据做处理，比如状态码的判断
    let { code, msg } = response.data
    if (code == 102) {
        vue.$message({ message: msg, type: 'error' })
        setTimeout(function () {
            vue.$router.push('/login');
        }, 3000)
    } else if (code !== 200) {
        throw new Error(msg);
    }
    return response
});


function request(url, data) {
    return service({
        method: 'post',
        url,
        data
    })
}


export default request